﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LogicComponents
{
    public class Wire
    {
        private bool _value;
        private Wire _originWire;

        public delegate void  valueChangedHandler () ;

        public event valueChangedHandler ValueChanged;

        public Wire()
        {
            
        }

        public Wire(Wire originWire)
        {
            setOriginWire(originWire);
        }

        public bool getValue ()
        {
            return _value;
        }

        public void setValue (bool value)
        {
            if (_originWire != null )
            {
                if (value!= _originWire.getValue() )
                {
                    throw new Exception("Can't change the value of a wire with an origin wire set to  a diferent value.");
                }
            }
            if (value != _value )
            {
                _value = value;
                if (ValueChanged != null )
                {
                    ValueChanged();
                }
            }
        }

        public Wire getOriginWire ()
        {
            return _originWire;
        }

        public void setOriginWire (Wire originWire)
        {
            _originWire = originWire;
            _originWire.ValueChanged += update;
            update();
        }

        public void clearOriginWire()
        {
            _originWire.ValueChanged -= update;
            _originWire = null;
        }

        public void update()
        {
            if (_originWire != null )
            {
                _value = _originWire.getValue() ;
                if (ValueChanged != null)
                {
                    ValueChanged();
                }
            }
            
        }


    }
}
